/**
 * Created by yy on 2017/8/25.
 */
$(function(){
    var url1 = location.search;
    if (url1.indexOf("?") != -1) {
        var id = url1.substr(4);}

    var url = server + '/server/business/workMessageRest/detail/'+id+'.json';
    $.post(url, function(data){
        if (data.code == 12000) {
            alert("登录超时");
            window.location.replace('../login.html');
        }
        var ctx = template('./check-work-tpl', data);
        $("#check-work").html(ctx);

        $('#btn').click(function () {
            var $state1 = $('#state1');
            var $state2 = $('#state2');
            var st1=$state1.text();
            var st2=$state2.text();
            var  usersLonAndLat = sessionStorage.getLonAndLat;

            var url2 = server+'/server/SignInAndSignOut/signInMessage.json';
            var json2 = {"workMessageId": messageid, "usersLonAndLat": usersLonAndLat};
            $.post(url2, json2, function (data) {
                if (data.code == 12000) {
                    alert("登录超时");
                    window.location.replace('../../login.html');
                }
                if(data.code == 11001|| data.code == 13102){

                }
                else {
                    if (data.message == "未到指定签退范围" || data.message == "未到指定签到范围") {
                        alert('不在打卡范围之内');
                    }
                }
            });
            if(st1 == "可签到") {
                var url3 = server+'/server/SignInAndSignOut/signIn.json';
                var json3 = {"workMessageId": messageid, "usersLonAndLat": usersLonAndLat}
                $.post(url3, json3, function (data) {
                    if (data.code == 12000) {
                        alert("登录超时");
                        window.location.replace('../../login.html');
                    }
                    if (data.state == "迟到") {

                        $state1.html('迟到');
                    }
                    else {   $state1.html('正常签到');
                    }
                });
            }

            if(st2 =="可签退") {
                var url4 = server+'/server/SignInAndSignOut/signOut.json';
                var json4 = {"workMessageId": messageid, "usersLonAndLat": usersLonAndLat};
                $.post(url4, json4, function (data) {
                    if (data.code == 12000) {
                        alert("登录超时");
                        window.location.replace('../../login.html');
                    }
                    var state2 = data.state.substr(3);
                    if (state2 == "早退") {
                        $state2.html('正常签退');
                    }
                    else { $state2.html('正常签退');
                    }
                });
            }

            window.location.reload();

        });
    });

});